/*
 * EchoServer.java
 */

package echoserver;


import java.io.*;
import java.net.*;

/**
   This program implements a simple server that listens to
   port 8189 and echoes back all client input.
*/
public class EchoServer
{  
   public static void main(String[] args )
   {  
      try
      {  
         // establish server socket
         ServerSocket s = new ServerSocket(8189);
         
         System.out.println("ECHO SERVER RUNNING...");

         // wait for client connection
         Socket incoming = s.accept();
         System.out.println("Accepted connection from client");
         
         
         BufferedReader s_in = new BufferedReader
            (new InputStreamReader(incoming.getInputStream()));
         PrintWriter s_out = new PrintWriter
            (incoming.getOutputStream(), true /* autoFlush */);
         s_out.println( "Hello! Enter BYE to exit." );
         

         // echo client input
         boolean done = false;
         while (!done)
         {  
            String line = s_in.readLine();
            
               s_out.println("Echo: " + line);
               
               if (line.trim().equals("BYE"))
                  done = true;
         }
         incoming.close();
      }
      catch (Exception e)
      {  
         e.printStackTrace();
      }
   }
}



